package com.darkplanet.management.dao;

import com.darkplanet.management.domain.PRMFAll;
import com.darkplanet.management.domain.Requirement;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class IRequirementimpl implements IRequirement{
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public void add(Integer project_id, String title, String simple_desc, String complex_desc, String remark) {
        String sql = "insert into requirement(project_id,title,simple_desc,complex_desc,remark) values (?,?,?,?,?)";
            try {
            queryRunner.update(sql,project_id,title,simple_desc,complex_desc,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer requirement_id) {
        String sql = "delete from requirement where requirement_id = ?";
        try {
            queryRunner.update(sql, requirement_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Integer project_id, Integer requirement_id, String title, String simple_desc, String complex_desc, String remark) {
        String sql = "update requirement set project_id=?,title=?,simple_desc=?,complex_desc=?,remark=? where requirement_id = ?";
        try {
            queryRunner.update(sql,project_id,title,simple_desc,complex_desc,remark,requirement_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Requirement getRequirementById(Integer requirement_id) {
        String sql = "select * from requirement where requirement_id = ?";
        Requirement requirement = null;
        try {
            requirement = queryRunner.query(sql,new BeanHandler<Requirement>(Requirement.class),requirement_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return requirement;

    }

    @Override
    public List<Requirement> getAllRequirement() {
        String sql = "select * from requirement";
        List<Requirement> query = null;
        try {
            query = queryRunner.query(sql,new BeanListHandler<Requirement>(Requirement.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public Requirement getRequirement_Id(String tittle) {
        String sql = "select * from requirement where tittle = ?";
        Requirement requirement = null;
        try {
            requirement = queryRunner.query(sql,new BeanHandler<Requirement>(Requirement.class),tittle);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return requirement;
    }

    @Override
    public List<PRMFAll> getThisMather() {
        String sql="SELECT * FROM requirement r left join project p on r.project_id = p.project_id";
        List<PRMFAll> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<PRMFAll>(PRMFAll.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
